<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>2_引出生命周期父组件</title>
</head>
<body>
    <div id="test">

    </div>
    <!-- 引入react核心库 -->
    <script type="text/javascript" src="../js/react.development.js"></script>
    <!-- 引入react-dom，用于支持react操作DOM -->
    <script type="text/javascript" src="../js/react-dom.development.js"></script>
    <!-- 引入babel，用于将jsx转为js -->
    <script type="text/javascript" src="../js/babel.min.js"></script>
    <script type="text/javascript" src="../js/prop-types.js"></script>
    <script type="text/babel">
        /* 
                1. 初始化阶段: 由ReactDOM.render()触发---初次渲染
                                    1. constructor()
                                    2. componentWillMount()
                                    3. render()
                                    4. componentDidMount() =====> 常用
                                                    一般在这个钩子中做一些初始化的事，例如：开启定时器、发送网络请求、订阅消息
                2. 更新阶段: 由组件内部this.setSate()或父组件render触发
                                    1. shouldComponentUpdate()
                                    2. componentWillUpdate()
                                    3. render() =====> 必须使用的一个
                                    4. componentDidUpdate()
                3. 卸载组件: 由ReactDOM.unmountComponentAtNode()触发
                                    1. componentWillUnmount()  =====> 常用
                                    一般在这个钩子中做一些收尾的事，例如：关闭定时器、取消订阅消息
        */

        class Life extends React.Component{
            constructor(props){
                super(props)
                console.log('--constructor--')
                this.state = {
                    opacity:1
                }
            }
            componentWillMount(){
                console.log('--componentWillMount--')
            }
            
            death = ()=>{
                console.log('--death--')
                ReactDOM.unmountComponentAtNode(document.getElementById('test'))
            }
            testSetState = ()=>{
                console.log('--testSetState--')
                let opacity = this.state.opacity
                opacity -= 0.1
                if(opacity<=0) opacity=1
                this.setState({
                    opacity
                })
            }
            forceTest = ()=>{
                this.forceUpdate()
            }
            shouldComponentUpdate(){
                console.log('--shouldComponentUpdate--')
                return true
            }

            componentWillUpdate(){
                console.log('--componentWillUpdate--')
            }
            render(){
                console.log('--render--');
                return(
                    <div>
                        <h2 style={{opacity:this.state.opacity}}>学习React太有趣了！</h2>
                        <button onClick={this.testSetState}>test setState</button>
                        <button onClick={this.death}>Distroy</button>
                        <button onClick={this.forceTest}>ForceUpdate</button>
                    </div>
                )
            }
            componentDidUpdate(){
                console.log('--componentDidUpdate--')
            }
            componentDidMount(){
                console.log('--componentDidMount--')
                /*
                this.timer = setInterval(()=>{
                    let {opacity} = this.state
                    opacity -= 0.1
                    if(opacity<=0) opacity=1
                    this.setState({
                        opacity:opacity
                    })
                },200)
                */
            }

            componentWillUnmount(){
                console.log('--componentWillUnmount--')
                //clearInterval(this.timer)
            }

        }
        
        class A extends React.Component{
            state = {
                car:'问界M8'
            }
            changeCar = ()=>{
                this.setState({
                    car:'二八大杠'
                })
            }
            render(){
                return(
                    <div>
                        <div>A {this.state.car}</div>
                        <button onClick={this.changeCar}>换车</button>
                        <B car={this.state.car}/>
                    </div>
                )
            }
        }

        class B extends React.Component{
            //初始化时不调用 变更的才能展示
            componentWillReceiveProps(props){
                console.log('B--componentWillReceiveProps--',props);
            }
            shouldComponentUpdate(){
                console.log('B--shouldComponentUpdate--');
                return true
            }
            componentWillUpdate(){
                console.log('B--componentWillUpdate--');
            }
            componentDidUpdate(){
                console.log('B--componentDidUpdate--');
            }
            componentWillUnmount(){
                console.log('B--componentWillUnmount--');
            }
            close = ()=>{
                ReactDOM.unmountComponentAtNode(document.getElementById('test'))
            }
            render(){
                console.log('B--render--')
                return(
                    <div>
                    B, show my car:{this.props.car}
                    <button onClick={this.close}> close test</button>
                    </div>
                    
                )
            }
        }
        

        ReactDOM.render(<A/>,document.getElementById('test'))
    </script>
</body>
</html>